import uuid
import os
from config import Config

from flask import jsonify
from werkzeug.utils import secure_filename


def response(code, message, data=None):
    result = {
        "code": code,
        "message": message,
        "data": data if data is not None else {}
    }
    return jsonify(result)


def upload_file(file, category):
    filename = f"{uuid.uuid4()}_{secure_filename(file.filename)}"
    upload_path = os.path.join(Config.UPLOAD_FOLDER + "/" + category + "/", filename)
    file.save(upload_path)
    return filename


def delete_file(filename, category):
    file_path = os.path.join(Config.UPLOAD_FOLDER + "/" + category + "/", filename)
    if not os.path.exists(file_path):
        return False
    try:
        os.remove(file_path)
        return True
    except Exception:
        return False
